package com.yifushidai.api;

import com.yifushidai.entity.ApiUniqueMac;
import com.yifushidai.mapper.ApiUniqueMacMapper;
import com.yifushidai.service.PowerService;
import com.yifushidai.utils.ErrorCodeEnum;
import com.yifushidai.utils.IpUtil;
import com.yifushidai.utils.R;
import com.yifushidai.utils.ShisuMinaPushUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

@RestController
@Api(tags = "设备推送")
@RequestMapping("/api/minapush")
public class MinaPushController {
    @Autowired
    private ApiUniqueMacMapper apiUniqueMacMapper;
    @Autowired
    private PowerService powerService;

    @PostMapping("/powerControlOn")
    @ApiOperation(value = "远程通电", notes = "拾宿版远程通电接口", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "mac", value = "联网盒mac", required = true)
    })
    public R powerControlOn(String mac, HttpServletRequest request) throws Exception {
        ApiUniqueMac one = apiUniqueMacMapper.selectOne(ApiUniqueMac.builder().mac(mac).build());
        if (null == one) return R.error(ErrorCodeEnum.MAC_NULL);

        Object res = ShisuMinaPushUtils.push(one.getPowerMac(), "powerControl:on");//隔10s发送取电指令，直到收到转发成功标记为止
        if ("OK".equals(res)) {
            powerService.savePower("远程", "on", one, IpUtil.getIpAddress(request));
            return R.ok();
        } else {
            return R.error(ErrorCodeEnum.MINA_PUSH_ERROR);
        }
    }

    @PostMapping("/powerControlOf")
    @ApiOperation(value = "远程断电", notes = "拾宿版远程断电接口", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "mac", value = "联网盒mac", required = true)
    })
    public R powerControlOf(String mac, HttpServletRequest request) throws Exception {
        ApiUniqueMac one = apiUniqueMacMapper.selectOne(ApiUniqueMac.builder().mac(mac).build());
        if (null == one) return R.error(ErrorCodeEnum.MAC_NULL);

        Object res = ShisuMinaPushUtils.push(one.getPowerMac(), "powerControl:of");//隔10s发送取电指令，直到收到转发成功标记为止
        if ("OK".equals(res)) {
            powerService.savePower("远程", "of", one, IpUtil.getIpAddress(request));
            return R.ok();
        } else {
            return R.error(ErrorCodeEnum.MINA_PUSH_ERROR);
        }
    }

    @PostMapping("/shisu")
    @ApiOperation(value = "拾宿版MINA推送接口", notes = "拾宿版MINA推送测试", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "mac", value = "联网盒mac", required = true),
            @ApiImplicitParam(paramType = "query", name = "strCmd", value = "字符指令", required = true)
    })
    public R shisu(String mac, String strCmd) throws Exception {
        ApiUniqueMac one = apiUniqueMacMapper.selectOne(ApiUniqueMac.builder().mac(mac).build());
        if (null == one) return R.error(ErrorCodeEnum.MAC_NULL);

        StringBuilder param = new StringBuilder();
        param.append(strCmd).append(" ");
        Object res = ShisuMinaPushUtils.push(mac, strCmd);
        if ("OK".equals(res)) {
            return R.ok();
        } else {
            return R.error(ErrorCodeEnum.MINA_PUSH_ERROR);
        }
    }

//    @PostMapping("/apartment")
//    @ApiOperation(value = "公寓版MINA推送接口", notes = "公寓版MINA推送测试", produces = MediaType.APPLICATION_JSON_VALUE)
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "query", name = "mac", value = "联网盒mac", required = true),
//            @ApiImplicitParam(paramType = "query", name = "hexCmd", value = "HEX指令", required = true),
//            @ApiImplicitParam(paramType = "query", name = "dest", value = "目的设备", required = true),
//            @ApiImplicitParam(paramType = "query", name = "msgId", value = "消息类型", required = true)
//    })
//    public R apartment(String mac, String hexCmd, String dest, String msgId, HttpServletRequest request) throws Exception {
//        Object res = ApartmentMinaPushUtils.push(mac, hexCmd, HexUtils.hexStringToBytes(dest)[0], HexUtils.hexStringToBytes(msgId)[0]);
//        return R.ok().put(res);
//    }
}
